var levelId = 0;
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
var memberIds =sessionStorage.getItem("memberId")
//详情
dimensionDetail()
function dimensionDetail() {
    $.ajax({
        url:root.member.getIntegralManageByMemberId,
        cache: false,  //禁用缓存
        contentType: 'application/x-www-form-urlencoded',
        type: "post",
        data: {
            memberId:memberIds

        },
        success: function (data) {
         if(data.errorCode==0){
             $("#loginaccount").html(data.data.loginAccount)
             $("#level").html(data.data.level)
             $("#waitIntegral").html(data.data.waitIntegral)
             $("#passIntegral").html(data.data.passIntegral)
             $("#expireIntegral").html(data.data.expireIntegral)
             }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}




// 查询分页表格数据
dimensionDetailTable()
function dimensionDetailTable() {
    $.ajax({
        url:root.member.listIntegralRecordByMemberId,
        cache: false,  //禁用缓存
        contentType: 'application/x-www-form-urlencoded',
        type: "post",
        data: {
            pageNum: 1,
            pageSize: 10,
            memberId:memberIds

        },
        success: function (data) {
            initTabls(data.data.result, 0);
            $('.M-box3').pagination({
                pageCount: data.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.member.listIntegralRecordByMemberId,
                        cache: false,  //禁用缓存
                        contentType: 'application/x-www-form-urlencoded',
                        type: "post",
                        data: {
                            pageNum: api.getCurrent(),
                            pageSize: 10,
                            memberId:memberIds

                        },
                        success: function (result) {
                            initTabls(result.data.result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}


// 渲染table
function initTabls(data) {
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组


        $.each(data, function (i, field) {
            var rowData = new Array();
            rowData.push(field.index);
            rowData.push(field.updateType);
            rowData.push(field.updateNum);
            rowData.push(field.updateReason);
            rowData.push(field.startTime);
            rowData.push(field.endTime);
            rowData.push(field.detailedText);
            rowData.push(field.clubName);
            rowData.push(field.clubClass);
            rowData.push(field.createTime);
            dataSource.push(rowData);

        });

    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#dimensionTables').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#dimensionTables").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if(row[1]==10){
                            val="收入"
                        }else {
                            val="支出"
                        }
                        if (row[1] == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },


                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val =""
                        if(row[3]==10){
                            val ="购物"
                        }else if(row[3]==20){
                            val ="订单取消"
                        }else if(row[3]==30){
                            val ="订单退货(积分增加)"
                        }else if(row[3]==40){
                            val ="订单抵扣"
                        }else if(row[3]==50){
                            val ="订单退货(积分扣减)"
                        }else if(row[3]==60){
                            val ="作废"
                        }else if(row[3]==70){
                            val ="手工调整"
                        }else if(row[3]==80){
                            val ="积分抽奖"
                        }else if(row[3]==90){
                            val ="积分换购"
                        }
                        if (row[3] == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[9];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets":4
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[4];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets":5
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[5];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets":6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[6]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[8]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 9
                },



            ]
        });
//权限控制函数
        power()
    };
}



/*跳转到添加页面*/
function jumpToAdd(title, url) {
    var waitIntegrals=$("#waitIntegral").html()
    sessionStorage.setItem("waitIntegral",waitIntegrals);
    sessionStorage.setItem("memberId",memberIds);
    levelId = 0;
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
